package Jastermind;

import Interface.JPanelAccueil;
import Various.Tools;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JOptionPane;

public class Game {

    private static final String fileName = "game.xml";
    private ArrayList<Color> combinaison;
    private ArrayList<Attempt> listAttemptsDone;
    private Player player;
    private String name;

    public Game(Player player) {
        this.name = "";
        this.player = player;
        this.combinaison = new ArrayList<Color>();
        this.createCombinaison();
        this.listAttemptsDone = new ArrayList<Attempt>();
    }

    private void createCombinaison() {
        Random random = new Random();
        while (this.combinaison.size() < this.player.getCombinaisonSize()) {
            Color currentColor = player.getListColors().get(random.nextInt(player.getListColors().size()));
            if (Tools.countColorInCombinaison(this.combinaison, currentColor) < this.player.getNbOccurence()) {
                this.combinaison.add(currentColor);
            }
        }
    }

    public Player getPlayer() {
        return player;
    }

    public ArrayList<Attempt> getListAttemptsDone() {
        return listAttemptsDone;
    }

    public ArrayList<Color> getCombinaison() {
        return combinaison;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return this.name;
    }

    public static String getFileName() {
        return fileName;
    }

    public void abandonGame() {
        String mess = "Si vous abandonnez la partie celle-ci sera considérée comme perdue";
        int abandon = JOptionPane.showConfirmDialog(null, mess);
        if (abandon == JOptionPane.OK_OPTION) {
            this.player.setGameLost(this.player.getGameLost() + 1);
            Appli.getJfMain().resetJFMainSettings();
            Appli.getJfMain().setJPContent(new JPanelAccueil());
        }
    }
}
